{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 简单的筛选条件（列表推导式或生成器表达式）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 10, 2, 3]\n",
      "[1, 16, 100, 4, 9]\n",
      "1 4 10 2 3 "
     ]
    }
   ],
   "source": [
    "mylist = [1, 4, -5, 10, -7, 2, 3, -1]\n",
    "print([n for n in mylist if n>0])\n",
    "\n",
    "# 对符合条件的元素做数据转换\n",
    "print([n*n for n in mylist if n>0])\n",
    "\n",
    "# 为了避免产生一个庞大的结果可以使用生成器表达式\n",
    "pos = (n for n in mylist if n>0)\n",
    "for x in pos:\n",
    "    print(x, end=' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 复杂的筛选条件（filter）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '2', '-3', '4', '5']\n"
     ]
    }
   ],
   "source": [
    "values = ['1', '2', '-3', '-', '4', 'N/A', '5']\n",
    "\n",
    "def is_int(val):\n",
    "    try:\n",
    "        x = int(val)\n",
    "        return True\n",
    "    except ValueError:\n",
    "        return False\n",
    "\n",
    "ivals = list(filter(is_int, values))\n",
    "print(ivals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 筛选条件在另一个序列中体现（itertools.compress）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'addresses' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-c83bee3a148a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mitertools\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcompress\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mmore5\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m>\u001b[0m\u001b[1;36m5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcounts\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcompress\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maddresses\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmore5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'addresses' is not defined"
     ]
    }
   ],
   "source": [
    "addresses = [\n",
    "    \"5412 N CLARK\",\n",
    "    \"5148 N CLARK\",\n",
    "    \"5800 N 58TH\",\n",
    "    \"2122 N CLARK\",\n",
    "]\n",
    "counts = [8, 3, 10, 4]\n",
    "from itertools import compress\n",
    "more5 = [n>5 for n in counts]\n",
    "list(compress(addresses, more5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
